package com.un.ebs.businessObject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.common.KeyValueDto;
import com.un.ebs.core.enums.ColumnRefTypeEnum;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.sys.domain.DataSource;
import com.un.ebs.sys.domain.Field;
import com.un.ebs.sys.dto.ColumnDto;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import com.un.ebs.sys.mapper.DataColumnMapper;
import com.un.ebs.sys.mapper.DataSourceMapper;
import com.un.ebs.sys.mapper.DataTableMapper;
import com.un.ebs.businessObject.service.DataColumnService;
import com.un.ebs.businessObject.service.DataFieldService;
import com.un.ebs.sys.domain.Column;
import com.un.ebs.sys.domain.Table;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author aikun
 * 服务实现类
 * @ClassName DataColumnServiceImpl
 * @Description DataColumnServiceImpl
 * @Date 2020/1/8
 */
@Service
public class DataColumnServiceImpl extends ServiceImpl<DataColumnMapper, Column> implements DataColumnService {
    @Autowired
    private DataColumnMapper dataColumnMapper;
    @Autowired
    private DataTableMapper dataTableMapper;
    @Autowired
    private DataFieldService dataFieldService;
    @Autowired
    private DataSourceMapper dataSourceMapper;

    @Override
    public List<ColumnDto> list(String tableId) {
        LambdaQueryWrapper<Column> lq = Wrappers.lambdaQuery();
        lq.eq(Column::getTableId, tableId);
        lq.orderByAsc(Column::getSort);
        List<Column> columnList = baseMapper.selectList(lq);
        List<ColumnDto> columnDtos = new ArrayList<>();
        for (Column column : columnList) {
            ColumnDto columnDto = new ColumnDto();
            if (null != column.getRefType()) {
                columnDto.setRefTypeIntVal(column.getRefType().getCode());
            }
            BeanUtils.copyProperties(column, columnDto);
            columnDtos.add(columnDto);
        }
        lq.orderByAsc(Column::getSort);
        return columnDtos;
    }

    @Override
    public void add(Column column) {
        Table table = dataTableMapper.selectById(column.getTableId());
        DataSource dataSource = dataSourceMapper.selectById(table.getDataSourceId());
        boolean lean = dataFieldService.isExistTable(table.getTableName(), dataSource.getName());
        if (lean) {
            Field field = checkData(column);
            dataFieldService.addField(field, table.getTableName(), dataSource.getName());
        }
        dataColumnMapper.insert(column);
    }

    @Override
    @Transactional
    public void update(Column column) {
        Table table = dataTableMapper.selectById(column.getTableId());
        DataSource dataSource = dataSourceMapper.selectById(table.getDataSourceId());
        Column column1 = dataColumnMapper.selectById(column.getId());
        dataColumnMapper.updateById(column);
        boolean lean = dataFieldService.isExistTable(table.getTableName(), dataSource.getName());
        if (lean) {
            Field field = checkData(column);
            dataFieldService.updateField(field, table.getTableName(), column1.getColumnName(), dataSource.getName());
        }
    }

    @Override
    public ColumnDto getById(String id) {
        Column column = dataColumnMapper.selectById(id);
        if (column == null) {
            throw new BusinessException("字段不存在");
        }
        ColumnDto columnDto = new ColumnDto();
        if (null != column.getRefType()) {
            columnDto.setRefTypeIntVal(column.getRefType().getCode());
        }
        BeanUtils.copyProperties(column, columnDto);
        return columnDto;
    }

    @Override
    @Transactional
    public void delete(String id) {
        Column column = dataColumnMapper.selectById(id);
        Table table = dataTableMapper.selectById(column.getTableId());
        DataSource dataSource = dataSourceMapper.selectById(table.getDataSourceId());
        dataColumnMapper.deleteById(id);
        boolean lean = dataFieldService.isExistTable(table.getTableName(), dataSource.getName());
        if (lean) {
            Field field = checkData(column);
            dataFieldService.delField(field, table.getTableName(), dataSource.getName());
        }
    }

    @Override
    public void updateDesc(String id, String columnDesc) {
        Column column = baseMapper.selectById(id);
        if (column == null) {
            throw new BusinessEntityNotFoundException(id, "列");
        }

        column.setColumnDesc(columnDesc);
        column.updateById();
    }

    @Override
    public void updateDesc(List<KeyValueDto> req) {
        for (KeyValueDto item : req) {
            updateDesc(item.getKey(), item.getValue());
        }
    }

    @Override
    public void changeSort(List<ObjectParentAndIndexChangeDto> req) {
        for (ObjectParentAndIndexChangeDto obj : req) {
            Column col = new Column();
            col.setId(obj.getId());
            col.setSort(obj.getIndex());
            baseMapper.updateById(col);
        }
    }

    //设置插入参数
    public Field checkData(Column column) {
        Field field = new Field();
        field.setFiledName(column.getColumnName());
        field.setFiledLength(column.getLength());
        field.setFiledType(column.getColumnType());
        field.setNull(column.getIsNullable());
        field.setPk(column.getIsPk());
        if (column.getColumnType().getCode() == ColumnRefTypeEnum.FK.getValue()) {
            field.setFkFieldName(column.getRefColumnName());
            field.setFkTableName(column.getRefTableName());
        }
        return field;
    }
}
